// pages/serve/serve.js
var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    radioCheckVal: 0,
    Page_Info:{},
    orderData:[]
  },
  gopay:function(e){
    if (e.currentTarget.dataset.isc) {
      console.log(e,'可以用')
      if (e.currentTarget.dataset.btnid == 1) {
        //去付款
        var memberInfo = wx.getStorageSync('memberInfo')
        var postArgs = {
          'Api_Type': '011',
          'orderId': e.currentTarget.dataset.id,
          'Member_Id': memberInfo.MemberId,
          'Member_Token': memberInfo.MemberToken
        }
        //获取
        getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
          (res) => {
            console.log(res, '支付信息')
            wx.requestPayment({
              timeStamp: res.Data.timeStamp,
              nonceStr: res.Data.nonceStr,
              package: res.Data.package,
              signType: res.Data.signType,
              paySign: res.Data.paySign,
              success: (res) => {
                console.log(res)
                wx.navigateTo({
                  url: '/pages/payed/payed?orderid=' + e.currentTarget.dataset.id,
                })
              },
              fail(res) { }
            })
          },
          (err) => {
            console.log(err.Message, '失败')
          }
        )
      } else if (e.currentTarget.dataset.btnid == 2) {
        //提交材料
        wx.navigateTo({
          url: '/pages/material/material?id=' + e.currentTarget.dataset.id,
        })
      }
     
    }else{

    console.log(e,'不能用')
    }
  },
  gotopay:function(e){ 
    
      if (e.currentTarget.dataset.btnid == 1){
          //去付款
          wx.navigateTo({
            url: '/pages/order/order?id=' + e.currentTarget.dataset.id,
          })
      } else if (e.currentTarget.dataset.btnid == 2){
         //提交材料
        wx.navigateTo({
          url: '/pages/material/material?id=' + e.currentTarget.dataset.id,
        })
      } else if (e.currentTarget.dataset.btnid == 3) {
        //已使用
        wx.navigateTo({ 
          url: '/pages/order/order?id=' + e.currentTarget.dataset.id,
        })
      } else if (e.currentTarget.dataset.btnid == 4) {
        //使用中
        wx.navigateTo({
          url: '/pages/order/order?id=' + e.currentTarget.dataset.id,
        })
      } else if (e.currentTarget.dataset.btnid == 0) {
        //已取消
        wx.navigateTo({
            url: '/pages/order/order?id=' + e.currentTarget.dataset.id,
          })
      }


    
    // console.log(e)
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      radioCheckVal: options.id,
      baseUrl: app.baseUrl
    })
    this.requestPost('015', 1, options.id)
  },
  radioCheckedChange: function (e) {
    var flag = parseInt(e.detail.value)
    if (this.data.radioCheckVal == flag){

    }else{
      this.setData({
        radioCheckVal: flag
      })
      console.log(flag)
      this.requestPost('015', 1, flag)
    }
    
  },

  //发送请求
  requestPost: function (apitype, pageIndex, stateid,index = 0) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': apitype,
      'pageIndex': pageIndex,
      'stateId': stateid,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
      , '').then(
        (res) => {
          console.log(res, '订单列表')
          if (res.Status == 2) {
            wx.clearStorage({
              success: () => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          } else if (res.Status == 0) {
            if (index == 0) {
              var c = res.Data.orderData
            } else if (index == 1) {
              console.log('加载更多')
              var a = this.data.orderData
              var c = a.concat(res.Data.orderData)
            }

            for (var i = 0; i < c.length; i++) {
              var item = c[i];
              item.SKU_Txt = item.SKU_Txt.split(/[，]/g);
              c[i] = item;
            }
            this.setData({
              Page_Info: res.Data.Page_Info,
              orderData: c
            })
          }
          
        },
        (err) => {
          console.log(err.Message, '产品列表信息失败')
        }
      )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (parseInt(this.data.Page_Info.CurPage) < parseInt(this.data.Page_Info.TotalPage)) {
      //请求数据
      var next = parseInt(this.data.Page_Info.CurPage) + 1
      this.requestPost('015', next, this.data.radioCheckVal,1)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})